package com.tudulist.service.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jersey.api.core.InjectParam;
import com.tudulist.dao.TodoDao;
import com.tudulist.dao.TodoListDao;
import com.tudulist.model.Todo;
import com.tudulist.rest.resource.TodoResource;

@Component
@Path("/todo")
public class TodoResourceServer implements TodoResource {

	@InjectParam
	TodoDao todoDao;

	@InjectParam
	TodoListDao todoListDao;

	@POST
	@Path("/add")
	@Consumes(MediaType.APPLICATION_XML)
	@Transactional
	public Todo createTodo(Todo todo, @QueryParam("todolistId") Long todolistId) {
		todo.setTodolists(todoListDao.find(todolistId));
		return todoDao.save(todo);
	}

	@GET
	@Path("{todolistId}")
	@Produces(MediaType.APPLICATION_XML)
	@Transactional
	public List<Todo> findAll(@PathParam("todolistId") Long todolistId) {
		return todoDao.findAll(todolistId);
	}

	@DELETE
	@Path("todoId")
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	@Transactional
	public void remove(@PathParam("todoId") long todoId) {
		todoDao.remove(todoId);
	}
}
